package com.langyiquan.front.websocket;

import com.langyiquan.utils.JsonUtils;
import io.netty.channel.*;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.AttributeKey;

import java.net.URLDecoder;

public class AuthRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

    public static AttributeKey<String> TOKEN = AttributeKey.valueOf("token");

    @Override
    public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
        String url = request.getUri();
        String temp[] = url.split(";");
        if (temp.length < 2) {
            ctx.close();
        }

        String token = URLDecoder.decode(temp[1], "UTF-8");
        if("langyiquan".equals(token)){
            request.setUri("/ws");
            ctx.fireChannelRead(request.retain());
        }else{
            ctx.writeAndFlush("token 无效").addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture channelFuture) throws Exception {
                    channelFuture.channel().close();
                }
            });
        }

    }
}